package org.sky.service.serviceImpl;

import org.sky.base.DaoSupport;
import org.sky.model.Room;
import org.sky.model.RoomUsed;
import org.sky.service.MealService;
import org.sky.service.RoomUsedService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-10-22
 * Time: 下午9:30
 * To change this template use File | Settings | File Templates.
 */

@Service
@Transactional
public class RoomUsedServiceImpl extends DaoSupport<RoomUsed> implements RoomUsedService {

    private MealService mealService;

    /**
     * 判断时间段内K房是否被使用
     *
     * @param room      房间
     * @param startTime 使用开始时间
     * @param endTime   使用结束时间
     * @return
     */
    public boolean isBooking(Room room, Date startTime, Date endTime) {
        Query query = em.createQuery("from RoomUsed r where r.startTime > ?1 and r.endTime < ?2 and r.room = ?3");
        query.setParameter(1, startTime).setParameter(2, endTime).setParameter(3, room);
        List<RoomUsed> ru = query.getResultList();
        if (ru.size() > 0)
            return true;
        else
            return false;
    }

    /**
     * 计算总房费
     *
     * @param roomId 房间编号
     * @return
     */
    public double totalMoney(int roomId) {
        Query query = em.createQuery("from RoomUsed r where r.room.roomId = ?1 and r.pay = 0 ");
        query.setParameter(1, roomId);
        RoomUsed ru = (RoomUsed) query.getResultList().get(0);

        Query roomQuery = em.createQuery("from Room r where r.roomId = ?1");
        roomQuery.setParameter(1, roomId);
        Room room = (Room) roomQuery.getResultList().get(0);

        if (ru.getMeal().getMealId() == 1) {
            return mealService.findPrice(room.getRoomStyle(), ru.getMeal().getMealName()) + ru.getExtraMoney();

        } else if (ru.getMeal().getMealId() == 2) {
            Date startTime = ru.getStartTime();
            Date endTime = ru.getEndTime();
            long l = endTime.getTime() - startTime.getTime();
            long hour = l / (60 * 60 * 1000);
            long min = ((l / (60 * 1000)) - hour * 60);
            System.out.println("" + hour + "小时" + min + "分钟");

            double price = mealService.findPrice(room.getRoomStyle(), ru.getMeal().getMealName());
            return hour * price + min * (price / 60.0) + ru.getExtraMoney();

        } else if (ru.getMeal().getMealId() == 3) {
            return ru.getSingerNum() * mealService.findPrice(room.getRoomStyle(), ru.getMeal().getMealName()) + ru.getExtraMoney();

        } else
            return 0.0d;
    }

    /**
     * 某时间段房间使用情况
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     */
    public List<RoomUsed> findByDate(Date begin, Date end) {
        Query query = em.createQuery("from RoomUsed r where r.createTime > ?1 and r.createTime < ?2 ");
        query.setParameter(1, begin).setParameter(2, end);
        return query.getResultList();
    }

    /**
     * 某时间段营业额
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     */
    public double revenue(Date begin, Date end) {
        Query query = em.createQuery("from RoomUsed r where r.createTime > ?1 and r.createTime < ?2 ");
        query.setParameter(1, begin).setParameter(2, end);
        List<RoomUsed> ru = query.getResultList();
        double sum = 0.0d;
        for (int i = 0; i < ru.size(); i++) {
            sum = sum + ru.get(i).getMoney();
        }
        return sum;
    }

    public MealService getMealService() {
        return mealService;
    }

    @Resource(name = "mealServiceImpl")
    public void setMealService(MealService mealService) {
        this.mealService = mealService;
    }
}
